#include <sys/types.h>       
#include<stdio.h>
#include <sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include <sys/epoll.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
#include<fcntl.h>
#include<signal.h>
#include<sys/time.h>
#include<pthread.h>
#include <ifaddrs.h>
#include<ctype.h>


#define TRUE  1
#define FALSE 0


typedef struct tcp_data
{
    struct sockaddr_in *sin;
    struct sockaddr_in *u_sin;
    int *sfd;
    struct sockaddr_in *u_cin;//客户端不需要u_cin
    int *u_sfd;
    int *udp_port;
    int *epoll_instance;
    struct epoll_event *ev;

}TCP;

char user_name[20];
char pass_word[20];
char buf[1024];
char u_buf[1024];
int login_ok;
int money;
int UDP_OPEN;

void my_home(TCP*d);


void GetGateWay(char gateway[])  
{  
    FILE *fp;  
    char buf[512];  
    char cmd[128]; 
    char *tmp;  

    strcpy(cmd, "ip route");  
    fp = popen(cmd, "r"); 
    if(NULL == fp)  
    {  
	perror("popen error");  
	return ;  
    }  
    while(fgets(buf, sizeof(buf), fp) != NULL)  
    {  
	tmp =buf;  
	while(*tmp && isspace(*tmp))  
	    ++ tmp;  
	if(strncmp(tmp, "default", strlen("default")) == 0)  
	    break;  
    }  
    sscanf(buf, "%*s%*s%s", gateway);         
    printf("本地网关:%s\n", gateway);  
    pclose(fp);  
}  



void Get_IP_addr (char ip_address[])
{
    struct ifaddrs * ifAddrStruct=NULL;
    void * tmpAddrPtr=NULL;

    char gateway[30];  
    getifaddrs(&ifAddrStruct);

 
    GetGateWay(gateway);
    
    while (ifAddrStruct!=NULL) 
    {
	if (ifAddrStruct->ifa_addr->sa_family==AF_INET)
	{   // check it is IP4
	    // is a valid IP4 Address
	    tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr;
	    char addressBuffer[INET_ADDRSTRLEN];
	    inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
	    if(0==strncmp(gateway,addressBuffer,12))
	    {
		printf("网卡标识:%s\n本地IP地址:%s\n", ifAddrStruct->ifa_name, addressBuffer); 
		strcpy(ip_address,addressBuffer);
		break;
	    }
	}
	ifAddrStruct=ifAddrStruct->ifa_next;
    }

//    return 0;
}


void int_to_str(int i,char s[])
{
    int a,b;
    char c,d;
    memset(s,0,2);
    a=i/10;
    b=i%10;
    c='0'+a;
    d='0'+b;
    strncpy(s,&c,1);
    strncat(s,&d,1);
}

void heart_time_pack(TCP*d)
{
    while(1)
    {
	if(login_ok)
	{
	    write(*d->sfd,"07heartpack",11);
	}
	sleep(5);
    }
}

void regist(TCP*d)
{
	printf("*****注册******\n");
	printf("用户名:");
	scanf("%s",user_name);
	printf("密  码:");
	scanf("%s",pass_word);
	memset(buf,0,1024);
	strncpy(buf,"02",2);//2是注册

	char s[2]={0};
	int_to_str(strlen(user_name),s);
	strncat(buf,s,2);
	int_to_str(strlen(pass_word),s);
	strncat(buf,s,2);
	strncpy(&buf[6],user_name,strlen(user_name));
	strncpy(&buf[6+strlen(user_name)],pass_word,strlen(pass_word));

	printf("注册中，请稍后。。。。\n");
	write(*d->sfd,buf,1024);

	while(1)
	{
	    if(0==strncmp(&buf[6+strlen(user_name)+strlen(pass_word)],"registok",8))
	    {
		printf("注册成功，请使用注册用户名和密码登录\n");
		    break;
	    }
	    else if(0==strncmp(&buf[6+strlen(user_name)+strlen(pass_word)],"registfail",10))
	    {
		printf("注册失败了，可能是用户名存在了哦，换一个试试吧\n");
		printf("***********\n");
		printf("用户名:");
		scanf("%s",user_name);
		printf("密  码:");
		scanf("%s",pass_word);
		memset(buf,0,1024);
		strncpy(buf,"02",2);//2是注册

		int_to_str(strlen(user_name),s);
		strncat(buf,s,2);
		int_to_str(strlen(pass_word),s);
		strncat(buf,s,2);
		strncpy(&buf[6],user_name,strlen(user_name));
		strncpy(&buf[6+strlen(user_name)],pass_word,strlen(pass_word));

		printf("注册中，请稍后。。。。\n");
		write(*d->sfd,buf,1024);
	    }
	}
}

void look_needer(TCP*d)
{


    printf("正在收集并上传你的位置信息，请稍后。。。\n");

    memset(buf,0,1024);
    strncpy(buf,"0",1);//第2位写请求类型，5是上传位置信息
    strncat(buf,"5",1);//第1位为0，则是出租车
    //第3～6位写用户和密码长度
    char s[3]={0};
    int_to_str(strlen(user_name),s);
    strncat(buf,s,2);
    int_to_str(strlen(pass_word),s);
    strncat(buf,s,2);

    strncat(buf,user_name,strlen(user_name));
    strncat(buf,pass_word,strlen(pass_word));

    int X;//经度
    int Y;//纬度
    
    //生成随机位置
    float i;
    float i1;
    srand((unsigned)time(0));      //根据时间来初始化开始rand（）
    i=(((float) rand()/RAND_MAX)*360); // 360 为最大的值，即i=（0～360）范围
    X=(int)i;

    
    int_to_str(X/100,s);
    strncat(buf,s,2);
    int_to_str(X%100,s);
    strncat(buf,s,2);
    
    sleep(1);

    srand((unsigned)time(0));      //根据时间来初始化开始rand（）
    i1=(((float) rand()/RAND_MAX)*360); // 360 为最大的值，即i=（0～360）范围
    Y=(int)i1;

    int_to_str(Y/100,s);
    strncat(buf,s,2);
    int_to_str(Y%100,s);
    strncat(buf,s,2);

    strncat(buf,"free",4);

    int_to_str(*d->udp_port/10000,s);
    strncat(buf,s,2);
    int_to_str((*d->udp_port%10000)/100,s);
    strncat(buf,s,2);
    int_to_str((*d->udp_port%10000)%100,s);
    strncat(buf,s,2);

    printf("你的位置为经度：%d，纬度：%d\n",X,Y);


    write(*d->sfd,buf,1024);


    while(1)//while是等待服务器发回信息
    {
	if(0==strncmp(&buf[1],"9",1))//服务器返回第1位是9，返回出租车信息
	{
	    break;
	}
    }

    if(0==strncmp(&buf[2],"0000",4))
    {
	printf("你附近没有人要打的哦，在等等吧\n");
	my_home(d);
    }
    else
    {
	printf("\n你附近想要打的的人如下\n");
	printf("**************************\n");

	int yue=1;
	int ret_len=0;
	char ubuf[20]={0};
	char gbuf[5]={0};
	char flen[3]={0};
	char dlen[3]={0};

	char from[60]={0};
	char towhere[60]={0};
	
	memset(ubuf,0,20);
	memset(gbuf,0,5);

	while(1)
	{
	    strncpy(s,&buf[2+ret_len],2);//用户名长度

	    ret_len=ret_len+2;
	    strncpy(ubuf,&buf[2+ret_len],atoi(s));//用户名

	    ret_len=ret_len+atoi(s);
	    strncpy(gbuf,&buf[2+ret_len],4);//距离，4个字节
	    ret_len=ret_len+4;

	    strncpy(flen,&buf[2+ret_len],2);//出发地长度，2个字节
	    ret_len=ret_len+2;
	    strncpy(dlen,&buf[2+ret_len],2);//目的地长度，2个字节
	    ret_len=ret_len+2;

	    strncpy(from,&buf[2+ret_len],atoi(flen));//出发地
	    ret_len=ret_len+atoi(flen);

	    strncpy(towhere,&buf[2+ret_len],atoi(dlen));//出发地
	    ret_len=ret_len+atoi(dlen);

	    printf("%d  %s，距离：%s，起点：%s，终点：%s\n",yue,ubuf,gbuf,from,towhere);

	    if(0==strncmp(&buf[2+ret_len],"0000",4))
	    {
		break;
	    }
	    yue++;
	}

	printf("%d  都不选，只想静静\n",yue+1);
	printf("**************************\n");
	printf("你想联系哪个人呢？选择对方编号就可以聊天了哦\n");

	int j;
	while(1)
	{
	    scanf("%d",&j);
	    if(j<yue+1&&j>0)
	    {
		ret_len=0;
		    yue=1;
		while(1)
		{

		    strncpy(s,&buf[2+ret_len],2);

		    ret_len=ret_len+2;
		    strncpy(ubuf,&buf[2+ret_len],atoi(s));

		    ret_len=ret_len+atoi(s);
		    strncpy(gbuf,&buf[2+ret_len],4);
		    ret_len=ret_len+4;

		    strncpy(flen,&buf[2+ret_len],2);//出发地长度，2个字节
		    ret_len=ret_len+2;
		    strncpy(dlen,&buf[2+ret_len],2);//目的地长度，2个字节
		    ret_len=ret_len+2;

		    strncpy(from,&buf[2+ret_len],atoi(flen));//出发地
		    ret_len=ret_len+atoi(flen);

		    strncpy(towhere,&buf[2+ret_len],atoi(dlen));//出发地
		    ret_len=ret_len+atoi(dlen);
		    
		    if(j==yue)
		    {
			break;
		    }
		    yue++;
		}
		memset(buf,0,1024);
		memset(gbuf,0,5);
		strncpy(gbuf,s,2);

		strncpy(buf,"0",1);//第2位写请求类型，8是约车/同意约车
		strncat(buf,"8",1);//第1位为0，则是出租车
		//第3～6位写用户和密码长度
		int_to_str(strlen(user_name),s);
		strncat(buf,s,2);
		int_to_str(strlen(pass_word),s);
		strncat(buf,s,2);

		strncpy(s,gbuf,2);
		strncat(buf,s,2);//要约的客户名的长度
		strncat(buf,ubuf,atoi(s));//要约的客户名

		write(*d->sfd,buf,1024);

		printf("正在从服务器获取信息。。。。\n");

		while(1)
		{
		    if(0==strncmp(&buf[1],"6",1))
		    {
			break;
		    }
		}

		
		//接收对方ip地址,ip地址最长为15位
		char u_port[7]={0};
		int n_port=0;
		yue=14;
		while(1)
		{
		    if(0==strncmp("X",&buf[4+atoi(s)+yue],1))
		    {
			strncpy(ubuf,&buf[4+atoi(s)],yue);
			strncpy(u_port,&buf[4+atoi(s)+yue+4],6);
			n_port=atoi(u_port);
			yue--;
		    }
		    else break;
		}


		int u_ret;
#if 1 
		printf("对方IP为=%s，端口是:%d\n",ubuf,n_port);
		printf("\n正在发送聊天请求，等待对方同意。。。。\n");
		printf("输入'结束等待'退出本次请求\n");
		d->u_cin->sin_family=AF_INET;
		d->u_cin->sin_port=htons(n_port);
		if(inet_pton(AF_INET,ubuf,&d->u_cin->sin_addr.s_addr) <=0 )
		{
		    perror("inet_pton");
		    return ;
		}
		//绑定地址和端口,绑定的是服务器的地址(设置约车人为服务器)
#endif

		//在线程中开启UDP后添加监听u_sfd和0

		UDP_OPEN=TRUE;//udp聊天标志为真，epoll监听有效

		d->ev->data.fd = *d->u_sfd;
		d->ev->events = EPOLLIN|EPOLLET;
		u_ret = epoll_ctl(*d->epoll_instance, EPOLL_CTL_ADD,*d->u_sfd,&(*d->ev));//添加监听udp通道
		d->ev->data.fd = 0;
		d->ev->events = EPOLLIN|EPOLLET;
		u_ret = epoll_ctl(*d->epoll_instance, EPOLL_CTL_ADD,0,&(*d->ev));//添加监听键盘

		int len =sizeof(*d->u_cin);
		memset(u_buf,0,1024);
		strncpy(u_buf,"wonderconnect",13);

		int_to_str(*d->udp_port/10000,s);
		strncat(u_buf,s,2);
		int_to_str((*d->udp_port%10000/100),s);
		strncat(u_buf,s,2);
		int_to_str((*d->udp_port%10000%100),s);
		strncat(u_buf,s,2);

		strcat(u_buf,user_name);
		u_ret=sendto(*d->u_sfd,u_buf,1024,0,(struct sockaddr*)d->u_cin,len);
		if(u_ret<=0)
		{
		    perror("sendto");
		}

		while(UDP_OPEN);//聊天中，一直循环

		if(0==strncmp(u_buf,"结束等待",12))
		{

		}
		else
		{
		    char yuecar;
		    printf("本次载客成功？(y/n)");

		    getchar();
		    yuecar=getchar();
		    if(yuecar=='y')
		    {
		    }
		    else
		    {
			write(*d->sfd,"06free",6);//状态置空闲，允许别人发来消息
		    }
		}
		u_ret=epoll_ctl(*d->epoll_instance, EPOLL_CTL_DEL,*d->u_sfd,&(*d->ev));//删除监听udp通道
		if(u_ret<0)
		{
		    perror("epoll_ctl删除UDP通道失败");
		}
		u_ret=epoll_ctl(*d->epoll_instance, EPOLL_CTL_DEL,0,&(*d->ev));//删除监听键盘
		if(u_ret<0)
		{
		    perror("epoll_ctl删除监听键盘失败");
		}

		my_home(d);
		break;
	    }
	    else if (j==yue+1)
	    {
		my_home(d);
	    }
	    else printf("做人坦荡荡，选择很重要！请重新选择！\n");
	}
    }
}

void my_home(TCP* d)
{
    printf("\n*****************\n");
    printf("当前用户:%s\n",user_name);
    printf("余额(元):%d\n",money);
    printf("*****************\n");
    printf("\n");
    printf("1.看谁要打的\n");//获取约车人信息
    printf("2.退出\n");
    int i;
    while(1)
    {
	scanf("%d",&i);

	if(1==i)
	{
	    look_needer(d);//转到查看约车人信息函数
	    break;
	}
	else if(2==i)
	{
	    char s[2]={0};
	    memset(buf,0,1024);
	    strncpy(buf,"04",2);
	    int_to_str(strlen(user_name),s);
	    strncat(buf,s,2);
	    int_to_str(strlen(pass_word),s);
	    strncat(buf,s,2);
	    strncpy(&buf[6],user_name,strlen(user_name));
	    strncpy(&buf[6+strlen(user_name)],pass_word,strlen(pass_word));
	    strncat(buf,"byebye",6);
	    write(*d->sfd,buf,1024);

	    return;
	}
	else printf("做人坦荡荡，选择很重要！请重新选择！\n");
    }
}

void login(TCP *d)
{
    printf("************\n");
    printf("用户名:");
    scanf("%s",user_name);
    printf("密  码:");
    scanf("%s",pass_word);
    memset(buf,0,1024);
    strncpy(buf,"03",2);
    char s[2]={0};
    int_to_str(strlen(user_name),s);
    strncat(buf,s,2);
    int_to_str(strlen(pass_word),s);
    strncat(buf,s,2);
    strncpy(&buf[6],user_name,strlen(user_name));
    strncpy(&buf[6+strlen(user_name)],pass_word,strlen(pass_word));

    write(*d->sfd,buf,1024);
    
    printf("验证中，请稍后。。。\n");
    login_ok=FALSE;
    int error_count=0;
    while(FALSE==login_ok)
    {
	if(0==strncmp("loginok",&buf[6+strlen(user_name)+strlen(pass_word)],7))
	{
	    printf("验证成功\n");
	    strncpy(s,&buf[6+strlen(user_name)+strlen(pass_word)+7],2);
	    money=atoi(s)*100;
	    strncpy(s,&buf[6+strlen(user_name)+strlen(pass_word)+7+2],2);
	    money=money+atoi(s);
	    login_ok=TRUE;
	}
	else if(0==strncmp("fail",&buf[6+strlen(user_name)+strlen(pass_word)],4))
	{
	    error_count++;
	    if(error_count>2)
	    {
		printf("验证失败，退出中。。。\n");
		return ;
	    }
	    printf("验证失败，请重新输入\n");
	    printf("************\n");
	    printf("用户名:");
	    scanf("%s",user_name);
	    printf("密  码:");
	    scanf("%s",pass_word);
	    memset(buf,0,1024);
	    strncpy(buf,"03",2);
	    int_to_str(strlen(user_name),s);
	    strncat(buf,s,2);
	    int_to_str(strlen(pass_word),s);
	    strncat(buf,s,2);
	    strncpy(&buf[6],user_name,strlen(user_name));
	    strncpy(&buf[6+strlen(user_name)],pass_word,strlen(pass_word));
	    write(*d->sfd,buf,1024);
	    login_ok=FALSE;
	    printf("验证中，请稍后。。。\n");
	}

    }
	my_home(d);//登录成功进入页面
}


void car(TCP*d)
{
    printf("*********************\n");
    printf("欢迎使用出租车客户端\n");
    printf("\n");
    printf("1.登录\n");
    printf("2.注册\n");
    printf("3.退出\n");
    int i;
    scanf("%d",&i);
    while(1)
    {
    
	if(1==i)
	{
	    login(d);
	    break;
	}
	else if(2==i)
	{
	    regist(d);
	    car(d);
	    break;
	}
	else if(3==i)
	{
	    char s[2]={0};
	    memset(buf,0,1024);
	    strncpy(buf,"04",2);
	    int_to_str(strlen(user_name),s);
	    strncat(buf,s,2);
	    int_to_str(strlen(pass_word),s);
	    strncat(buf,s,2);
	    strncpy(&buf[6],user_name,strlen(user_name));
	    strncpy(&buf[6+strlen(user_name)],pass_word,strlen(pass_word));
	    strncat(buf,"byebye",6);
	    write(*d->sfd,buf,1024);
	    break;
	}
	else 
	{
	    printf("做人坦荡荡，选择很重要！请重新选择！\n");
	    scanf("%d",&i);
	}
    }
    printf("TCP服务已关闭\n");
}

void* heart_pack(void*d)
{
    heart_time_pack((TCP*)d);
    return d;
}

void *car_index(void*d)
{
    car((TCP*)d);
    return d;
}

int main()
{
    system("clear");
    
    login_ok=FALSE;//登录标志
    UDP_OPEN=FALSE;//UDP开启标志
    money=0;
    int i;
    int ret;

    pthread_t pthid;//主页面线程ID
    pthread_t ppid;//心跳包线程ID
    TCP d;//传递线程结构体变量

    char ip_address[1024];
    Get_IP_addr(ip_address);//获取本地ip

    //epoll data
    int epoll_instance;
    int nfound;
    struct epoll_event ev,events[20];

//************TCP连接*****************//
    int sfd;
    struct sockaddr_in sin;

    //建立套接字，确定要使用的协议家族
    sfd = socket(AF_INET,SOCK_STREAM,0);
    if(sfd == -1)
    {
	perror("socket");
	return -1;
    }

    //绑定地址和端口
    sin.sin_family = AF_INET;
    sin.sin_port = htons(5000);
    //if(inet_pton(AF_INET,"192.168.188.57",&sin.sin_addr.s_addr) <=0 )//ip地址是服务器ip地址
    if(inet_pton(AF_INET,ip_address,&sin.sin_addr.s_addr) <=0 )//ip地址是服务器ip地址
    {
	perror("inet_pton");
	return -1;
    }
    ret = connect(sfd,(struct sockaddr*)&sin,sizeof(sin));
    if(ret == -1)
    {
	perror("连接服务器失败");
	return -1;
    }
    else 
    {
	printf("成功连接服务器!\n");
    }

//*************TCP连接结束*********************//


//**********UDP连接开始**********************//

    int u_sfd;
    int udp_port=3000;
    struct sockaddr_in u_sin;
    struct sockaddr_in u_cin;
    unsigned int len;
  
    //建立套接字，确定要使用的协议家族
    u_sfd = socket(AF_INET,SOCK_DGRAM,0);
    if(u_sfd == -1)
    {
	perror("socket");
	return -1;
    }

    while(1)
    {
	//绑定地址和端口
	u_sin.sin_family = AF_INET;
	u_sin.sin_port = htons(udp_port);
	if(inet_pton(AF_INET,ip_address,&u_sin.sin_addr.s_addr) <=0 )
	{
	    perror("inet_pton");
	    return -1;
	}
	ret = bind(u_sfd,(struct sockaddr*)&u_sin,sizeof(u_sin));
	if(ret == -1)
	{
	    udp_port++;
	}
	else break;

    }

//***************UDP连接结束***************************//
   
    
    d.sin=&sin;
    d.u_sin=&u_sin;
    d.sfd=&sfd;
    d.udp_port=&udp_port;

    d.u_cin=&u_cin;
    d.u_sfd=&u_sfd;
    
    d.epoll_instance=&epoll_instance;
    d.ev=&ev;

    pthread_create(&pthid,NULL,car_index,(void*)&d);
    pthread_create(&ppid,NULL,heart_pack,(void*)&d);

    epoll_instance = epoll_create(256);
    if(epoll_instance == -1)
    {
	perror("epoll_create");
	return -1;
    }

    //epoll set
    ev.data.fd = sfd;
    ev.events = EPOLLIN|EPOLLET;
    ret = epoll_ctl(epoll_instance, EPOLL_CTL_ADD,sfd,&ev);

//在线程中开启UDP后添加监听u_sfd和0
#if 0
    ev.data.fd = u_sfd;
    ev.events = EPOLLIN|EPOLLET;
    ret = epoll_ctl(epoll_instance, EPOLL_CTL_ADD,u_sfd,&ev);
    ev.data.fd = 0;
    ev.events = EPOLLIN|EPOLLET;
    ret = epoll_ctl(epoll_instance, EPOLL_CTL_ADD,0,&ev);
#endif

    while(1)
    {
	nfound = epoll_wait(epoll_instance,events,20,-1);
	if(nfound < 0)
	{
	    perror("epoll_wait");
	    continue;
	}
	else if(nfound == 0)
	{
	    continue;
	}
	else
	{
	    //循环遍历文件描述符号集合
	    for(i=0;i<nfound ;i++)
	    {
		if(events[i].events == EPOLLIN|EPOLLET && events[i].data.fd == sfd)
		{
		    ret = read(events[i].data.fd,buf,1024);
		    if(ret <=0)
		    {
			epoll_ctl(epoll_instance,EPOLL_CTL_DEL,events[i].data.fd,&events[i]);	
			continue;
		    }
		    if(0==strncmp(buf,"goodbyeok",9))
		    {
			login_ok=FALSE;
			sleep(1);
			printf("客户端退出\n");
			return 0;
		    }
		}//end if(sfd) 
		else if(TRUE==UDP_OPEN&&events[i].events == EPOLLIN|EPOLLET && events[i].data.fd == u_sfd)
		{
		    len=sizeof(u_cin);
		    ret = recvfrom(events[i].data.fd,u_buf,1024,0,(struct sockaddr*)&u_cin,&len);
		    if(ret <0)
		    {
			perror("recvfrom");
			continue;
		    }

		    if(0==strncmp(u_buf,"wonderconnect",13))
		    {

		    }
		    else if(0==strncmp(u_buf,"denyconnect",11))
		    {
			write(1,"对方未同意聊天\n",22);
			UDP_OPEN=FALSE;
			continue;

		    }
		    else if(0==strncmp(u_buf,"agreeconnect",12))
		    {
			write(sfd,"06full",6);//状态置空闲，允许别人发来消息
			write(1,"对方同意连接,可以聊天了\n",35);
			continue;
		    }
		    else if(0==strncmp(u_buf,"对方已离开",15))
		    {
			write(1,u_buf,ret);
			UDP_OPEN=FALSE;
			continue;
		    }
		    else
		    {
			write(1,u_buf,ret);
			continue;
		    }
		}//end if(u_sfd)
		else if(TRUE==UDP_OPEN&&events[i].events == EPOLLIN|EPOLLET && events[i].data.fd == 0)
		{
		    ret =read(events[i].data.fd,u_buf,1024);
		    if(ret<0)
		    {
			perror("read");
			continue;
		    }
		    if(0==strncmp(u_buf,"结束聊天",12))
		    {
			UDP_OPEN=FALSE;//聊天结束
			memset(u_buf,0,1024);
			strncpy(u_buf,"对方已离开\n",16);
			len =sizeof(u_cin);
			sendto(u_sfd,u_buf,16,0,(struct sockaddr*)&u_cin,len);
			continue;
		    }
		    else if(0==strncmp(u_buf,"结束等待",12))
		    {

			UDP_OPEN=FALSE;//聊天结束
			continue;
		    }
		    len =sizeof(u_cin);
		    sendto(u_sfd,u_buf,ret,0,(struct sockaddr*)&u_cin,len);
		    continue;
		}//end if(0) 
	    }//end for 
	}//if(n <0)
    }//end while(1)
    close(sfd);
}

